Show language: C# VB.NET Both

Programmatic Searching

To run programmatic searches is fairly straight forward, it involves setting the Configuration to the correct Index Directory and using SearchAgent to run the actual search.

Example Searcher

C#
using System;
using Keyoti.SearchEngine;
using Keyoti.SearchEngine.Search;


public class MyClass
{
	public static void Main()
	{
			
		// Put user code to initialize the page here
		Configuration configuration = new Configuration();
		configuration.IndexDirectory =
		@"C:\...path to your index directory...";
		
		//this SearchUnit license key will expire December 2019, evaluators should email support
		//for a key
		string licensekey =
		"5C576C63695F6B646D5E6F59514A5628454147473D453F4446424847495247543B41412";
		
		//run a search for "the"
		SearchAgent sa = new SearchAgent(licensekey, configuration);

		//retrieve results 1 through 10
		SearchResult results = sa.Search("the", 1, 10);
		
		//the number of results there actually are in total
		int totalNumberOfResults = results.NumberOfResults;
		
		foreach(ResultItem i in results)
		{
			//the summary from the document
			string summary = i.Summary;
		
			//the title of the document
			string title = i.Title;
		
			//the document URL
			string url = i.UriString;
		
		
			//the weight of the document in the results
			int weight = i.Weight;
			Console.WriteLine(url);
		}
	}
}
VB.NET
Imports System
Imports Keyoti.SearchEngine
Imports Keyoti.SearchEngine.Search


Public Class MyClass1
    
    Public Shared Sub Main()
        ' Put user code to initialize the page here
        Dim configuration As Configuration = New Configuration
        configuration.IndexDirectory = "C:\...path to your index directory..."
        'this SearchUnit license key will expire December 2019, evaluators should email support
        'for a key
        Dim licensekey As String = "5C576C63695F6B646D5E6F59514A5628454147473D453F4446424847495247543B41412"
        'run a search for "the"
        Dim sa As SearchAgent = New SearchAgent(licensekey, configuration)

        'retrieve results 1 through 10
        Dim results As SearchResult = sa.Search("the", 1, 10)
        'the number of results there actually are in total
        Dim totalNumberOfResults As Integer = results.NumberOfResults
        For Each i As ResultItem In results
            'the summary from the document
            Dim summary As String = i.Summary
            'the title of the document
            Dim title As String = i.Title
            'the document URL
            Dim url As String = i.UriString
            'the weight of the document in the results
            Dim weight As Integer = i.Weight
            Console.WriteLine(url)
        Next
    End Sub
End Class

Using Custom Data Filters

Custom Data Filters allow the user to get results based not only on keyword matches but also filtered according to certain fields. For example if the indexed documents have a Custom Data field called 'price', then a Custom Data Filter can be specified to restrict searches to price ranges. SearchUnit already includes web based widgets for filtering results based on Custom Data, however it is also possible to filter results programmatically if you are using SearchAgent.

In this example below we are assuming that there already exists an index containing documents with the Custom Data fields 'vol' and 'price'.

C#
using System;
using Keyoti.SearchEngine;
using Keyoti.SearchEngine.Search;


public class MyClass
{
	public static void Main()
	{
			
		// Put user code to initialize the page here
		Configuration configuration = new Configuration();
		configuration.IndexDirectory =
		@"C:\...path to your index directory...";
		
		//this SearchUnit license key will expire December 2019, evaluators should email support
		//for a key
		string licensekey =
		"5C576C63695F6B646D5E6F59514A5628454147473D453F4446424847495247543B41412";
		
		SearchAgent sa = new SearchAgent(this.licensekey, Configuration);

		SearchResult r;
		//Create the collection of filters that will hold the different filters that we want to use		
		//Info about different filter types is available here 
		var filters = new FilterCollection(1);

		//First filter, allow results where the 'vol' field is '30' or '40'
		var customDataFilter = new CustomDataFilter();
		customDataFilter.FieldName = "vol";
		customDataFilter.Filter = new string[2][];
		customDataFilter.Filter[0] = new string[]{ "30", null };
		customDataFilter.Filter[1] = new string[]{"40", null};
		customDataFilter.Type = "stringor";
		filters.Add(customDataFilter);

		//Second filter, allow results where the 'price' field is '300'
		customDataFilter = new CustomDataFilter();
		customDataFilter.FieldName = "price";
		customDataFilter.Filter = new string[1][];
		customDataFilter.Filter[0] = new string[] { "300", null };
		customDataFilter.Type = "stringor";
		filters.Add(customDataFilter);





		var options = new SearchOptions(null, null, null, filters, null);
		results = sa.Search(query, options, 1, 10);

		
		//the number of results there actually are in total
		int totalNumberOfResults = results.NumberOfResults;
		
		foreach(ResultItem i in results)
		{
			//the summary from the document
			string summary = i.Summary;
		
			//the title of the document
			string title = i.Title;
		
			//the document URL
			string url = i.UriString;
		
		
			//the weight of the document in the results
			int weight = i.Weight;
			Console.WriteLine(url);
		}
	}
}